from gobang import Gobang, WHITE, BLACK

import pygame
import pygame.draw as draw

grid_size = 30
background = (255,200,150)
frontground = (0,0,255)
linecolor = (100,100,100)
chess_colors = {WHITE:(255,255,255), BLACK:(0,0,0)}
edge_width = 10

def get_screen_size(board_size):
    return ((board_size+1)*grid_size+2*edge_width, (board_size+1)*grid_size+2*edge_width)

def clear_screen(screen):
    screen.fill(background)

def draw_board(screen, gobang):
    clear_screen(screen)
    size = gobang.board_size + 1
    lines = size + 1
    for i in xrange(lines):
        draw.line(screen, linecolor, (edge_width, edge_width + i * grid_size),\
                 (edge_width + size * grid_size, edge_width + i * grid_size))
        draw.line(screen, linecolor, (edge_width + i * grid_size, edge_width),\
                 (edge_width + i * grid_size, edge_width + size * grid_size))
    draw_chess(screen, gobang)

def to_pixel_pos(board_pos):
    return ((board_pos[0]+1)*grid_size+edge_width, (board_pos[1]+1)*grid_size+edge_width)

def to_grid_pos(screen_pos, gobang):
    x, y = screen_pos
    x = (x - edge_width)/float(grid_size) - 1
    y = (y - edge_width)/float(grid_size) - 1
    i, j = int(round(x)), int(round(y))
    if abs(x - i) < 0.3 and abs(y - j) < 0.3 and \
       0 < i < gobang.board_size and 0 < j < gobang.board_size:
        return (i, j)

def draw_chess(screen, gobang):
    board = gobang.board
    cols, rows = board.shape
    chess_radius = grid_size / 2 - 2
    for c in xrange(cols):
        for r in xrange(rows):
            v = board[(c,r)]
            if v in chess_colors:
                draw.circle(screen, chess_colors[v], to_pixel_pos((c, r)), chess_radius)